package com.burstly.lib.component.networkcomponent.burstly.html.ormma.listener;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import com.burstly.lib.component.networkcomponent.burstly.html.ormma.OrmmaSensorController;
import com.burstly.lib.util.LoggerExt;
import java.util.List;

public class AccelListener
  implements SensorEventListener
{
  private static final int FORCE_THRESHOLD = 1000;
  private static final LoggerExt LOG = LoggerExt.getInstance();
  private static final int SHAKE_COUNT = 2;
  private static final int SHAKE_DURATION = 2000;
  private static final int SHAKE_TIMEOUT = 500;
  private static final String TAG = "ORMMA AccelListener";
  private static final int TIME_THRESHOLD = 100;
  private boolean mAccReady;
  private float[] mAccVals = { 0.0F, 0.0F, 0.0F };
  private float[] mActualOrientation = { -1.0F, -1.0F, -1.0F };
  private float[] mLastAccVals = { 0.0F, 0.0F, 0.0F };
  private long mLastForce;
  private long mLastShake;
  private long mLastTime;
  private boolean mMagReady;
  private float[] mMagVals;
  private int mRegisteredHeadingListeners = 0;
  private int mRegisteredShakeListeners = 0;
  private int mRegisteredTiltListeners = 0;
  private final OrmmaSensorController mSensorController;
  private int mSensorDelay = 3;
  private final SensorManager mSensorManager;
  private int mShakeCount;

  public AccelListener(Context paramContext, OrmmaSensorController paramOrmmaSensorController)
  {
    this.mSensorController = paramOrmmaSensorController;
    this.mSensorManager = ((SensorManager)paramContext.getSystemService("sensor"));
  }

  private void start()
  {
    List localList = this.mSensorManager.getSensorList(1);
    if (localList.size() > 0)
      this.mSensorManager.registerListener(this, (Sensor)localList.get(0), this.mSensorDelay);
  }

  private void startMag()
  {
    List localList = this.mSensorManager.getSensorList(2);
    if (localList.size() > 0)
    {
      this.mSensorManager.registerListener(this, (Sensor)localList.get(0), this.mSensorDelay);
      start();
    }
  }

  public float getHeading()
  {
    return this.mActualOrientation[0];
  }

  public void onAccuracyChanged(Sensor paramSensor, int paramInt)
  {
  }

  public void onSensorChanged(SensorEvent paramSensorEvent)
  {
    switch (paramSensorEvent.sensor.getType())
    {
    default:
    case 2:
    case 1:
    }
    while (true)
    {
      if ((this.mMagVals != null) && (this.mAccVals != null) && (this.mAccReady) && (this.mMagReady))
      {
        this.mAccReady = false;
        this.mMagReady = false;
        float[] arrayOfFloat = new float[9];
        SensorManager.getRotationMatrix(arrayOfFloat, new float[9], this.mAccVals, this.mMagVals);
        this.mActualOrientation = new float[3];
        SensorManager.getOrientation(arrayOfFloat, this.mActualOrientation);
        this.mSensorController.onHeadingChange(this.mActualOrientation[0]);
      }
      if (paramSensorEvent.sensor.getType() == 1)
      {
        long l1 = System.currentTimeMillis();
        if (l1 - this.mLastForce > 500L)
          this.mShakeCount = 0;
        if (l1 - this.mLastTime > 100L)
        {
          long l2 = l1 - this.mLastTime;
          if (10000.0F * (Math.abs(this.mAccVals[0] + this.mAccVals[1] + this.mAccVals[2] - this.mLastAccVals[0] - this.mLastAccVals[1] - this.mLastAccVals[2]) / (float)l2) > 1000.0F)
          {
            int i = 1 + this.mShakeCount;
            this.mShakeCount = i;
            if ((i >= 2) && (l1 - this.mLastShake > 2000L))
            {
              this.mLastShake = l1;
              this.mShakeCount = 0;
              this.mSensorController.onShake();
            }
            this.mLastForce = l1;
          }
          this.mLastTime = l1;
          this.mSensorController.onTilt(this.mAccVals[0], this.mAccVals[1], this.mAccVals[2]);
        }
      }
      return;
      this.mMagVals = ((float[])paramSensorEvent.values.clone());
      this.mMagReady = true;
      continue;
      this.mLastAccVals = this.mAccVals;
      this.mAccVals = ((float[])paramSensorEvent.values.clone());
      this.mAccReady = true;
    }
  }

  public void setSensorDelay(int paramInt)
  {
    this.mSensorDelay = paramInt;
    if ((this.mRegisteredTiltListeners > 0) || (this.mRegisteredShakeListeners > 0))
    {
      stop();
      start();
    }
  }

  public void startTrackingHeading()
  {
    if (this.mRegisteredHeadingListeners == 0)
      startMag();
    this.mRegisteredHeadingListeners = (1 + this.mRegisteredHeadingListeners);
  }

  public void startTrackingShake()
  {
    if (this.mRegisteredShakeListeners == 0)
    {
      setSensorDelay(1);
      start();
    }
    this.mRegisteredShakeListeners = (1 + this.mRegisteredShakeListeners);
  }

  public void startTrackingTilt()
  {
    if (this.mRegisteredTiltListeners == 0)
      start();
    this.mRegisteredTiltListeners = (1 + this.mRegisteredTiltListeners);
  }

  public void stop()
  {
    if ((this.mRegisteredHeadingListeners == 0) && (this.mRegisteredShakeListeners == 0) && (this.mRegisteredTiltListeners == 0))
      this.mSensorManager.unregisterListener(this);
  }

  public void stopAllListeners()
  {
    this.mRegisteredTiltListeners = 0;
    this.mRegisteredShakeListeners = 0;
    this.mRegisteredHeadingListeners = 0;
    try
    {
      stop();
      return;
    }
    catch (Exception localException)
    {
      LOG.logError("ORMMA AccelListener", "Runtime exception in stop()", new Object[0]);
    }
  }

  public void stopTrackingHeading()
  {
    if (this.mRegisteredHeadingListeners > 0)
    {
      int i = -1 + this.mRegisteredHeadingListeners;
      this.mRegisteredHeadingListeners = i;
      if (i == 0)
        stop();
    }
  }

  public void stopTrackingShake()
  {
    if (this.mRegisteredShakeListeners > 0)
    {
      int i = -1 + this.mRegisteredShakeListeners;
      this.mRegisteredShakeListeners = i;
      if (i == 0)
      {
        setSensorDelay(3);
        stop();
      }
    }
  }

  public void stopTrackingTilt()
  {
    if (this.mRegisteredTiltListeners > 0)
    {
      int i = -1 + this.mRegisteredTiltListeners;
      this.mRegisteredTiltListeners = i;
      if (i == 0)
        stop();
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.component.networkcomponent.burstly.html.ormma.listener.AccelListener
 * JD-Core Version:    0.6.2
 */